package com.xfzp.android.ms;

import java.nio.ShortBuffer;

import javax.microedition.khronos.opengles.GL10;

import android.opengl.Matrix;

import com.xfzp.d3.Coord;
import com.xfzp.d3.Ray;
import com.xfzp.d3.Shape;

/**
 * 
 * @author AndroidApe
 * @version 2011-12-7
 */
public class KubeSprite {
	private Main main;
	private Kube kube;
	private AbleLayer ableLayer;

	private float[] modelView;// 模型观察矩阵

	private Ray ray;// 拾取射线
	private float downX;
	private float downY;
	private Coord downCoord;
	private boolean unPickup;
	private boolean hasUp = false;

	/**
	 * 
	 * @param main
	 * @param cubeOrderStr
	 *            小立方体顺序
	 * @param viewPointStr
	 *            视点坐标
	 */
	public KubeSprite(Main main, String cubeOrderStr, String modelViewStr) {
		kube = new Kube(parseCubeOrder(cubeOrderStr));
		modelView = parseModelView(modelViewStr);
		this.main = main;
	}

	/**
	 * 返回模型观察矩阵
	 * 
	 * @return
	 */
	public float[] getModelView() {
		return modelView;
	}

	/**
	 * 返回层顺序
	 * 
	 * @return
	 */
	public int[] getCubeOrder() {
		return kube.getCubeOrder();
	}

	/**
	 * 画魔方。如果动画执行完成，则返回true；否则返回false
	 * 
	 * @param gl
	 * @return
	 */
	public boolean draw(GL10 gl) {
		boolean r = animation();
		for (Shape s : kube.shapes()) {
			gl.glVertexPointer(3, GL10.GL_FLOAT, 0, s.vertexBuffer());
			gl.glColorPointer(4, GL10.GL_FLOAT, 0, s.colorBuffer());

			ShortBuffer ib = s.indexBuffer();
			gl.glDrawElements(GL10.GL_TRIANGLES, ib.remaining(),
					GL10.GL_UNSIGNED_SHORT, ib);
		}
		return r;
	}

	public void down(float x, float y, Ray pickRay) {
		downX = x;
		downY = y;
		ray = pickRay;
		downCoord = ray.getOrigin();
		unPickup = true;
		ableLayer = null;
		hasUp = false;
	}

	public void move(float x, float y, Coord end) {
		Coord rotDir = end.subtract(downCoord);
		downCoord = end;
		if (unPickup) {
			unPickup = false;
			ableLayer = kube.rayPickup(ray, rotDir);
		}
		if (ableLayer != null) {
			ableLayer.rotateAngle(rotDir);
			return;
		}
		Matrix.rotateM(modelView, 0, (y - downY) / 2, 1, 0, 0);// 沿X轴旋转
		Matrix.rotateM(modelView, 0, (x - downX) / 2, 0, 1, 0);// 沿Y轴旋转
		downY = y;
		downX = x;
	}

	public boolean up() {
		if (ableLayer != null) {
			ableLayer.setAuto();
			hasUp = true;
			return true;
		}
		return false;
	}

	/**
	 * 执行动画，如果动画执行完成，则返回true；否则返回false
	 * 
	 * @return
	 */
	private boolean animation() {
		if (ableLayer != null) {
			int rc = kube.roundAbleLayer(ableLayer);
			if (rc > 0)
				main.addRotateSum(rc);
			return rc == -1 && hasUp;
		}
		return true;
	}

	/**
	 * 解析小立方体的顺序
	 * 
	 * @param cubeOrderStr
	 * @return
	 */
	private int[] parseCubeOrder(String cubeOrderStr) {
		if (cubeOrderStr != null) {
			String[] cos = cubeOrderStr.split(",");
			if (cos.length != 26)
				return null;
			int[] cubeOrder = new int[26];
			for (int i = 0; i < 26; i++) {
				try {
					cubeOrder[i] = Integer.parseInt(cos[i]);
				} catch (Exception e) {
					return null;
				}
			}
			return cubeOrder;
		}
		return null;
	}

	/**
	 * 解析模型观察矩阵
	 * 
	 * @param modelViewStr
	 * @return
	 */
	private float[] parseModelView(String modelViewStr) {
		float[] m = new float[16];
		if (modelViewStr != null) {
			String[] cos = modelViewStr.split(",");
			if (cos.length == 16) {
				boolean b = true;
				for (int i = 0; i < 16; i++) {
					try {
						m[i] = Float.parseFloat(cos[i]);
					} catch (Exception e) {
						b = false;
						break;
					}
				}
				if (b)
					return m;
			}
		}
		Matrix.setIdentityM(m, 0);
		Matrix.translateM(m, 0, 0, 0, -3.0f);// 转化
		Matrix.scaleM(m, 0, 0.5f, 0.5f, 0.5f);// 缩放
		Matrix.rotateM(m, 0, 35, 1, 0, 0);// 沿X轴旋转35度
		Matrix.rotateM(m, 0, -45, 0, 1, 0);// 沿Y轴旋转-45度
		return m;
	}

}
